﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Runtime.Serialization;
using System.ServiceModel;

using SSATool.Common.Util;
using SSATool.DL.Common;
using SSATool.Common.Logging;

#endregion

namespace SSATool.DL.Server
{
    /// <summary>
    /// Represents the admin WCF server instance.
    /// </summary>
    public class ServiceAdminWcfServer : DisposeBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private ServiceHost _adminSvcHost;

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Starts the WCF server.
        /// </summary>
        public void Start()
        {
            // create the binding for an endpoint.
            NetTcpBinding binding = new NetTcpBinding();
            binding.Security.Mode = SecurityMode.Transport;
            binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

            // create the ServiceHost for a calculator.
            Uri baseUri = new Uri("net.tcp://" + ServiceContext.Current.ServiceUrl);
            Uri[] baseAddresses = new Uri[] { baseUri };
            _adminSvcHost = new ServiceHost(typeof(ServiceAdminService), baseAddresses);
            
            // add an endpoint using the binding and a new address.
            Type contract = typeof(IServiceAdminService);
            _adminSvcHost.AddServiceEndpoint(contract, binding, "ServiceAdminService");
            _adminSvcHost.Open();
        }

        /// <summary>
        /// Stops the WCF server.
        /// </summary>
        public void Stop()
        {
            try
            {
                if (_adminSvcHost != null)
                    _adminSvcHost.Close();
            }
            catch (CommunicationObjectFaultedException) // ignore this exception, it is thrown if the service has not been started yet
            {
                ((IDisposable)_adminSvcHost).Dispose();
            }
        }

        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected override void ReleaseManagedResources()
        {
            Stop();
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
